<?php

class ContactController extends \BaseController {
    protected $layout = 'layouts.admin_panel';
    
    public function __construct(Contact $contact) {
        parent::__construct();
        $this->contact = $contact;
    }
    
    /**
     * Display Document list
     * @author Thuan Truong
     * @return responses
     */
    public function index() {
        $contacts = $this->contact->getContactsList();

        $view = View::make('contact.index')->with(array(
            'contacts' => $contacts,
        ));
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.contacts.index'));
        } else {
            $this->layout->content = $view;
        }
    }
    
    /**
     * Display contact page
     * @author Thuan Truong
     */
    public function contact() {
        $this->layout = View::make('layouts.application');
        
        $view = View::make('contact.contact');
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.contacts.index'));
        } else {
            $this->layout->content = $view;
        }
    }
    
    /**
     * Store contact 
     * @param input
     * @return response
     * @author Thuan Truong
     */
    public function store() {
        $input = array_map('trim', Input::all());
        $validator = $this->contact->validateContact($input);
        if($validator->passes()){
            $contact = $this->contact->store($input);
            if (isset($contact->id) && !empty($contact->id)) {
            	// Send contact info to cs@mulberrylane.com
            	$email = new Email();
            	$body_contact = View::make('emails.user_contact')->with('contact', $contact);
            	$email->add(Lang::get('email.subject.user_contact'), Config::get('config.mail_info.from'), Config::get('config.mail_info.server_mail'), $body_contact);
            }
            
            Session::flash('f_notice', Lang::get('contact.action.create.done'));
            return Response::json(array(
                'status' => 1,
                'redirect' => route('contact'),
            ));
        } else {
            $arr_error = $validator->messages()->getMessages();
            if ($this->language_code == 'en') {
                    foreach($arr_error as $key=>$err) {
                        $str_err = str_replace('validation.captcha', Lang::get('user.error.validate_captcha'), $err);
                        $arr_error[$key] = $str_err;
                    }
                }
            if ($this->language_code == 'vi') {
                foreach($arr_error as $key=>$err) {
                    $str_err = str_replace('name', Lang::get('contact.error.name'), $err);
                    $str_err = str_replace('email', Lang::get('contact.error.email'), $str_err);
                    $str_err = str_replace('phone', Lang::get('contact.error.phone'), $str_err);
                    $str_err = str_replace('content', Lang::get('contact.error.content'), $str_err);
                    $str_err = str_replace('address', Lang::get('contact.error.address'), $str_err);
                    $str_err = str_replace('validation.captcha', Lang::get('user.error.validate_captcha'), $str_err);
                    $arr_error[$key] = $str_err;
                }
            }
            if (Request::ajax()) {
                return Response::json(array(
                    'status' => 0, 
                    'code' => 'invalid_data', 
                    'messages' => $arr_error));
            } else {
                return Redirect::action('ContactController@contact');
            }
        }       
    }
    
    
    /**
     * Show detail contact
     * @author Thuan Truong
     * @param  int  $id
     * @return Response
     */
    public function view($id) {
        $contact = $this->contact->findContactById($id);
        $language_code = $this->language_code;
    
        $view = View::make('contact.view')->with(array(
            'contact' => $contact,
            'language_code' => $language_code
        ));
        if (Request::ajax()) {
            return $this->ajaxResponse($view, Lang::get('nav.page.contacts.view'));
        } else {
            $this->layout->content = $view;
        }
    }
    
    /**
     * Delete document 
     * @author Thuan Truong
     * @param id
     * @return response
     */
    public function destroy($id) {
        $this->contact->deleteContact($id);
        if (Request::wantsJson()) {
            return Response::json(array(
                'status' => 1,
                'message' => Lang::get('contact.action.delete.done')
            ));
        } else {
            return Redirect::action('ContactController@index')->with('f_notice', Lang::get('contact.action.delete.done'));
        }
    }
    
}